package com.document.controller;

import com.document.common.Result;
import com.document.dto.LoginDTO;
import com.document.dto.RegisterDTO;
import com.document.service.UserService;
import com.document.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    @PostMapping("/register")
    public Result<Map<String, String>> register(@Validated @RequestBody RegisterDTO registerDTO) {
        userService.register(registerDTO);

        Map<String, String> result = new HashMap<>();
        result.put("message", "注册成功");
        return Result.success(result);
    }

    @PostMapping("/login")
    public Result<Map<String, String>> login(@Validated @RequestBody LoginDTO loginDTO) {
        String token = userService.login(loginDTO);

        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        result.put("message", "登录成功");
        return Result.success(result);
    }

    @GetMapping("/profile")
    public Result<UserVO> getProfile() {
        UserVO userVO = userService.getCurrentUser();
        return Result.success(userVO);
    }
}